﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.Opinions;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Web.Navigation;

namespace blackhouse.Aparts.Web.View {

    [Serializable]
    public class DisplayOpinion : IOpinion {

        #region Fields

        internal IOpinion opinion;
        internal IApartmentGroup apartment;

        #endregion

        #region Properties

        public string Country { get; set; }
        public string PublishedDateFormatted { get; set; }
        public string Answer { get; set; }
        public bool HaveAnswer { get; set; }
        public string InPlusOpinionFormatted { get; set; }
        public string InMinusOpinionFormatted { get; set; }
        public string AverageRatingFormatted { get; set; }
        public string AvatarImageUrl { get; set; }
        public string ApartmentNavigateUrl { get; set; }
        public string ApartmentText { get; set; }
        public string ApartmentToolTip { get; set; }
        public bool IsPublic { get; private set; }


        public int Id { get; set; }
        public string InMinusOpinion { get; set; }
        public string InPlusOpinion { get; set; }
        public DateTime PublishedDate { get; set; }
        public AuthorOpinion Publisher { get; set; }
        public string LinkToOpinion { get; set; }

        #endregion

        #region Constuctors

        public DisplayOpinion() { }

        public DisplayOpinion(IOpinion opinion) {
            this.opinion = opinion;
            this.SetCountry();
            this.SetPublishedDateFormatted();
            this.SetInPlusOpinionFormatted();
            this.SetInMinusOpinionFormatted();
            this.SetAverageRatingFormatted();
            this.SetAvatarImageUrl();
            this.SetApartment();
            this.SetUrlToOpinion();

            this.Id = this.opinion.Id;
            this.InMinusOpinion = this.opinion.InMinusOpinion;
            this.InPlusOpinion = this.opinion.InPlusOpinion;
            this.PublishedDate = this.opinion.PublishedDate;
            this.Publisher = this.opinion.Publisher;
            this.IsPublic = this.opinion.IsPublic;
            this.Answer = this.opinion.Answer;
            this.HaveAnswer = !string.IsNullOrEmpty(this.Answer);
        }

        #endregion

        #region Methods

        private void SetUrlToOpinion() {
            this.LinkToOpinion = UrlUtils.LocalizeUrl(NavigationProvider.Instance().GetUrl(this.opinion));
        }

        private void SetInPlusOpinionFormatted() {
            if (!string.IsNullOrEmpty(this.opinion.InPlusOpinion))
                this.InPlusOpinionFormatted = this.opinion.InPlusOpinion;
            else
                this.InPlusOpinionFormatted = HttpContext.GetGlobalResourceObject("Opinions", "noPositive").ToString();
        }

        private void SetInMinusOpinionFormatted() {
            if (!string.IsNullOrEmpty(this.opinion.InMinusOpinion))
                this.InMinusOpinionFormatted = this.opinion.InMinusOpinion;
            else
                this.InMinusOpinionFormatted = HttpContext.GetGlobalResourceObject("Opinions", "noNegative").ToString();
        }

        private void SetAverageRatingFormatted() {
            this.AverageRatingFormatted = this.opinion.GetAverageRating().ToString("F2");
        }

        private void SetAvatarImageUrl() {
            string fileName = this.opinion.Publisher.Category.ToLower().ReplaceEx(" ", "-").ReplaceEx("(", "-").ReplaceEx(")", "-");
            fileName = "/Data/SiteImages/Person/" + fileName + ".jpg";
            FileInfo avatarFile = new FileInfo(HttpContext.Current.Server.MapPath(fileName));
            if (avatarFile.Exists)
                this.AvatarImageUrl = fileName;
            else
                this.AvatarImageUrl = "/Data/SiteImages/Person/anonim.jpg";
        }

        private void SetApartment() {
            this.apartment = this.opinion.GetAssociatedApartmentGroup();
            if (this.apartment != null) {
                this.ApartmentNavigateUrl = blackhouse.mojoPortalExtend.PageProvider.AdjustUrlToCurrentSite(this.apartment.Url);
                this.ApartmentText = this.apartment.ShortName;
                this.ApartmentToolTip = this.apartment.Name;
            }
        }

        private void SetPublishedDateFormatted() {
            this.PublishedDateFormatted = this.opinion.PublishedDate.ToString("D");
        }

        private void SetCountry() {
            RegionInfo ri = RegionInfoExtend.FindRegionInfoByISO(this.opinion.Publisher.Country);
            if (ri != null)
                this.Country = ri.DisplayName;
        }

        public void AddOpinionDetailValue(int categoryId, float val) {
            throw new NotImplementedException();
        }

        public int FindReservationToAddOpinion(int apartmentTypeId) {
            throw new NotImplementedException();
        }

        public IApartmentGroup GetAssociatedApartmentGroup() {
            return this.apartment;
        }

        public int GetAssociatedApartmentGroupId() {
            throw new NotImplementedException();
        }

        public float GetAverageRating() {
            throw new NotImplementedException();
        }

        public float GetOpinionDetailValue(int categoryId) {
            throw new NotImplementedException();
        }

        public IOpinion[] GetOpinions() {
            throw new NotImplementedException();
        }

        public IOpinion[] GetOpinions(bool b) {
            throw new NotImplementedException();
        }

        public void Publish(int userId) {
            throw new NotImplementedException();
        }

        public bool Save(int userId) {
            throw new NotImplementedException();
        }

        public IOpinion[] GetSimilarOpinions(int opinionId, TimeSpan timeWatch) {
            throw new NotImplementedException();
        }

        public void Visit(IOpinionVisitor visitor) {
            throw new NotImplementedException();
        }

        public bool Moderate(int userId) {
            throw new NotImplementedException();
        }

        public List<IWebReservation> FindInterviewerReservation() {
            return new List<IWebReservation>();
        }

        #endregion

    }
}